<?php

namespace App\Modules\Admin\Services\Property;

use App\Exceptions\Exception;
use App\Modules\Admin\Entities\Property\MaintenanceModel;
use App\Modules\Admin\Entities\Property\MaintenanceRecordModel;
use App\Modules\Admin\Services\BaseService;
use Illuminate\Support\Facades\DB;

class MaintenanceRecordService extends BaseService
{
    public function __construct(MaintenanceRecordModel $master)
    {
        $this->model = $master;

    }
    public function lists(array $params): array
    {
        $params['where_callback'] = function($query) use ($params){
            $request = request();
            // 按照名称进行搜索
            if (!empty($search = $request->input('search', ''))){
                $query->where('meter_name', 'LIKE', '%' . trim($search) . '%');
            } // apparatus_id
            if (!empty($community_id = $request->input('community_id', ''))){
                $query->where('community_id', $community_id);
            }
            if (!empty($apparatus_id = $request->input('apparatus_id', ''))){
                $query->where('apparatus_id', $apparatus_id);
            }
            if (!empty($type = $request->input('type', ''))){
                $query->where('type', $type);
            }

            // 是否公开
            $type = $request->input('type', -1);
            if ($type > -1){
                $query->where('type', '=', $type);
            }
        };
        return parent::lists($params); // TODO: Change the autogenerated stub
    }
    public function create(array $params)
    {
        DB::beginTransaction();
        try{
            parent::create($params); // TODO: Change the autogenerated stub
            DB::commit();
            return true;
        }catch (Exception $e){
            DB::rollBack();
            throw new Exception($e->getMessage());
        }
    }

}
